package com.sailfish.springbootdemo.dao.db1;

import com.sailfish.springbootdemo.common.DB1TableName;
import com.sailfish.springbootdemo.pojo.db1.PcbaCompany;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface PcbaCompanyDao {

    /**
     * 查询所有的
     *
     * @param searchValue
     * @return
     */
    @Select("<script>" +
            "select * from " + DB1TableName.PCBA_COMPANY + " where 1 = 1 " +
            "<if test='searchValue != null and searchValue != \"\" '>" +
            " and pcba_company_name_cn like CONCAT('%',#{searchValue},'%') or pcba_company_name_en like CONCAT('%',#{searchValue},'%')" +
            "</if>" +
            "<if test='belongPcba != null and belongPcba != \"\" '>" +
            " and pcba_company_id = #{belongPcba}" +
            "</if>" +
            " order by sort" +
            "</script>")
    @Results(id = "pcbaCompanyResultMap", value = {
            @Result(column = "pcba_company_id", property = "pcbaCompanyId", jdbcType = JdbcType.INTEGER),
            @Result(column = "pcba_company_name_cn", property = "pcbaCompanyNameCn", jdbcType = JdbcType.VARCHAR),
            @Result(column = "pcba_company_name_en", property = "pcbaCompanyNameEn", jdbcType = JdbcType.VARCHAR),
            @Result(column = "sort", property = "sort", jdbcType = JdbcType.INTEGER),
            @Result(column = "status", property = "status", jdbcType = JdbcType.INTEGER),
            @Result(column = "notes", property = "notes", jdbcType = JdbcType.VARCHAR),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "update_time", property = "updateTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "pcba_company_code", property = "pcbaCompanyCode", jdbcType = JdbcType.VARCHAR),
            @Result(column = "email_owners", property = "emailOwners", jdbcType = JdbcType.VARCHAR),
            @Result(column = "email_owners2", property = "emailOwners2", jdbcType = JdbcType.VARCHAR),
            @Result(column = "email_owners3", property = "emailOwners3", jdbcType = JdbcType.VARCHAR),
            @Result(column = "email_owners4", property = "emailOwners4", jdbcType = JdbcType.VARCHAR),
            @Result(column = "email_owners5", property = "emailOwners5", jdbcType = JdbcType.VARCHAR),
            @Result(column = "email_owners6", property = "emailOwners6", jdbcType = JdbcType.VARCHAR),
    })
    List<PcbaCompany> getAllCompany(@Param("searchValue") String searchValue, @Param("belongPcba") String belongPcba);


    /**
     * 删除
     *
     * @param pcbaCompanyId
     * @return
     */
    @Delete("delete from " + DB1TableName.PCBA_COMPANY + " where pcba_company_id = #{pcbaCompanyId}")
    Integer deletePcbaCompany(@Param("pcbaCompanyId") Integer pcbaCompanyId);

    /**
     * 根据id查询
     *
     * @param pcbaCompanyId
     * @return
     */
    @ResultMap(value = "pcbaCompanyResultMap")
    @Select("select * from " + DB1TableName.PCBA_COMPANY + " where pcba_company_id = #{pcbaCompanyId}")
    PcbaCompany getPcbaCompanyById(@Param("pcbaCompanyId") Integer pcbaCompanyId);

    /**
     * 判断有没有重复的
     *
     * @param pcbaCompanyId
     * @return
     */
    @ResultMap(value = "pcbaCompanyResultMap")
    @Select("<script>" +
            "select * from " + DB1TableName.PCBA_COMPANY + " where pcba_company_name_cn = #{pcbaCompanyNameCn} " +
            "<if test='pcbaCompanyId != null and pcbaCompanyId != \"\" '>" +
            " and pcba_company_id != #{pcbaCompanyId}" +
            "</if>" +
            "</script>")
    List<PcbaCompany> judgeUniquePcbaCompany(@Param("pcbaCompanyId") Integer pcbaCompanyId, @Param("pcbaCompanyNameCn") String pcbaCompanyNameCn);

    /**
     * 新增
     *
     * @param pcbaCompany
     * @return
     */
    @Insert("insert into " + DB1TableName.PCBA_COMPANY + "(pcba_company_name_cn,pcba_company_name_en,status,sort,create_time,notes,pcba_company_code) values " +
            "(#{pcbaCompany.pcbaCompanyNameCn},#{pcbaCompany.pcbaCompanyNameEn},#{pcbaCompany.status},#{pcbaCompany.sort},#{pcbaCompany.createTime},#{pcbaCompany.notes},#{pcbaCompany.pcbaCompanyCode})")
    Integer addPcbaCompany(@Param("pcbaCompany") PcbaCompany pcbaCompany);

    @ResultMap(value = "pcbaCompanyResultMap")
    @Select("select * from " + DB1TableName.PCBA_COMPANY + " where pcba_company_name_cn = #{pcbaCompanyNameCn}")
    List<PcbaCompany> getPcbaCompanyByName(@Param("pcbaCompanyNameCn") String pcbaCompanyNameCn);

    /**
     * 更新
     *
     * @param pcbaCompany
     * @return
     */
    @Update("<script>" +
            "update " + DB1TableName.PCBA_COMPANY +
            "<set>" +
            "<if test='pcbaCompany.pcbaCompanyNameCn != null and pcbaCompany.pcbaCompanyNameCn != \"\" '>" +
            " pcba_company_name_cn = #{pcbaCompany.pcbaCompanyNameCn}," +
            "</if>" +
            "<if test='pcbaCompany.pcbaCompanyNameEn != null and pcbaCompany.pcbaCompanyNameEn != \"\" '>" +
            " pcba_company_name_en = #{pcbaCompany.pcbaCompanyNameEn}," +
            "</if>" +
            "<if test='pcbaCompany.status != null and pcbaCompany.status != \"\" '>" +
            " status = #{pcbaCompany.status}," +
            "</if>" +
            "<if test='pcbaCompany.sort != null and pcbaCompany.sort != \"\" '>" +
            " sort = #{pcbaCompany.sort}," +
            "</if>" +
            "<if test='pcbaCompany.notes != null and pcbaCompany.notes != \"\" '>" +
            " notes = #{pcbaCompany.notes}," +
            "</if>" +
            "<if test='pcbaCompany.pcbaCompanyCode != null and pcbaCompany.pcbaCompanyCode != \"\" '>" +
            " pcba_company_code = #{pcbaCompany.pcbaCompanyCode}," +
            "</if>" +
            "<if test='pcbaCompany.updateTime != null '>" +
            " update_time = #{pcbaCompany.updateTime}," +
            "</if>" +
            "</set>" +
            " where pcba_company_id = #{pcbaCompany.pcbaCompanyId}" +
            "</script>")
    Integer updatePcbaCompany(@Param("pcbaCompany") PcbaCompany pcbaCompany);

    /**
     * 更新 工厂的 发送邮件负责人
     *
     * @param pcbaCompanyId
     * @param emailOwners
     * @return
     */
    @Update("update " + DB1TableName.PCBA_COMPANY + " set email_owners = #{emailOwners} where pcba_company_id = #{pcbaCompanyId}")
    Integer updateEmailOwners(@Param("pcbaCompanyId") Integer pcbaCompanyId, @Param("emailOwners") String emailOwners);

    @Update("update " + DB1TableName.PCBA_COMPANY + " set email_owners2 = #{emailOwners2} where pcba_company_id = #{pcbaCompanyId}")
    Integer updateEmailOwners2(@Param("pcbaCompanyId") Integer pcbaCompanyId, @Param("emailOwners2") String emailOwners2);

    @Update("update " + DB1TableName.PCBA_COMPANY + " set email_owners3 = #{emailOwners3} where pcba_company_id = #{pcbaCompanyId}")
    Integer updateEmailOwners3(@Param("pcbaCompanyId") Integer pcbaCompanyId, @Param("emailOwners3") String emailOwners3);

    @Update("update " + DB1TableName.PCBA_COMPANY + " set email_owners4 = #{emailOwners4} where pcba_company_id = #{pcbaCompanyId}")
    Integer updateEmailOwners4(@Param("pcbaCompanyId") Integer pcbaCompanyId, @Param("emailOwners4") String emailOwners4);

    @Update("update " + DB1TableName.PCBA_COMPANY + " set email_owners5 = #{emailOwners5} where pcba_company_id = #{pcbaCompanyId}")
    Integer updateEmailOwners5(@Param("pcbaCompanyId") Integer pcbaCompanyId, @Param("emailOwners5") String emailOwners5);

    @Update("update " + DB1TableName.PCBA_COMPANY + " set email_owners6 = #{emailOwners6} where pcba_company_id = #{pcbaCompanyId}")
    Integer updateEmailOwners6(@Param("pcbaCompanyId") Integer pcbaCompanyId, @Param("emailOwners6") String emailOwners6);
}
